﻿namespace FlexiCommerce.Web.UI.DynamicData
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Xml;

    using FlexiCommerce.Persistence.NH;

    public class FlexiModelProvider : System.Web.DynamicData.ModelProviders.DataModelProvider
    {
        #region Fields

        System.Collections.ObjectModel.ReadOnlyCollection<System.Web.DynamicData.ModelProviders.TableProvider> tables;

        #endregion Fields

        #region Constructors

        public FlexiModelProvider()
        {
            IList<System.Web.DynamicData.ModelProviders.TableProvider> temp = new List<System.Web.DynamicData.ModelProviders.TableProvider>();
            System.Reflection.Assembly asm = Assembly.GetExecutingAssembly();
            System.IO.Stream xmlStream = asm.GetManifestResourceStream("FlexiCommerce.Mappings.Default.hbm.xml");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlStream);
            foreach (XmlNode node in xmlDoc.ChildNodes[1].ChildNodes)
            {
                if (node.Name == "class")
                {
                    temp.Add(new FlexiCommerce.Web.UI.DynamicData.FlexiTableProvider(this, node));
                }
            }
            tables = new System.Collections.ObjectModel.ReadOnlyCollection<System.Web.DynamicData.ModelProviders.TableProvider>(temp);
        }

        #endregion Constructors

        #region Properties

        public override System.Collections.ObjectModel.ReadOnlyCollection<System.Web.DynamicData.ModelProviders.TableProvider> Tables
        {
            get {
                return tables;
            }
        }

        #endregion Properties

        #region Methods

        public override object CreateContext()
        {
            throw new NotImplementedException();
        }

        #endregion Methods
    }
}